# -*- coding:utf-8 -*-
class Solution:
    def IsPopOrder(self, pushV, popV):
        # write code here
        # if len(pushV) != len(popV):
        #     return False
        # stack = [pushV[0]]
        # pop_index = 0
        # push_index = 1
        # while pop_index < len(popV):
        #     if pop_index == 0:
        #         if popV[pop_index] != stack[-1] :
        #             if push_index < len(pushV):
        #                 stack.append(pushV[push_index])
        #                 push_index += 1
        #             else:
        #                 return False
        #         else:
        #             stack.pop()
        #             pop_index += 1
        #     else:
        #         if popV[pop_index] != stack[-1] and popV[pop_index] in stack:
        #             return False
        #         elif popV[pop_index] not in stack:
        #             if push_index < len(pushV):
        #                 stack.append(pushV[push_index])
        #                 push_index += 1
        #             else:
        #                 return False
        #         else:
        #             stack.pop()
        #             pop_index += 1
        # return True
        if not len(pushV):
            return False
        stack = []
        j = 0
        for i in range(len(pushV)):
            stack.append(pushV[i])
            while j < len(popV) and (len(stack) and popV[j] == stack[-1]):
                stack.pop()
                j += 1
        return len(stack) == 0



